from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, FloatField, BooleanField, PasswordField, SelectField, DateField
from wtforms.validators import DataRequired, Length

from app.models import User


class LoginForm(FlaskForm):
    mobile = StringField('手机号', validators=[DataRequired()], render_kw={'placeholder': '输入管理员手机号'})
    password = PasswordField('密码', validators=[DataRequired(), Length(6, 128, message='密码长度好像不太对')],
                             render_kw={'placeholder': '密码'})
    remember = BooleanField('记住我')
    submit = SubmitField('登录')


class SettingForm(FlaskForm):
    title = StringField('网站标题', validators=[DataRequired()])
    name = StringField('收款姓名', validators=[DataRequired()])
    bank = StringField('支行信息', validators=[DataRequired()])
    number = StringField('银行卡号', validators=[DataRequired()])
    submit = SubmitField('提交')


class UserEditForm(FlaskForm):
    name = StringField('真实姓名', validators=[DataRequired()])
    mobile = StringField('手机号', validators=[DataRequired()])
    submit = SubmitField('提交')


class LoanEditForm(FlaskForm):
    count = FloatField('借款金额', validators=[DataRequired()])
    status = BooleanField('已还款')
    deadline = DateField('还款日期', validators=[DataRequired()])
    user = SelectField('选择用户', coerce=int, default=1)
    submit = SubmitField('提交')

    def __init__(self, *args, **kwargs):
        super(LoanEditForm, self).__init__(*args, **kwargs)
        self.user.choices = [(user.id, user.name)
                             for user in User.query.all()]
